package com.changgou.center.controller;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.order.feign.CollectFeign;
import com.changgou.order.feign.OrderFeign;
import com.changgou.order.pojo.Collect;
import com.changgou.order.pojo.Order;
import com.changgou.user.feign.AddressFeign;
import com.changgou.user.feign.UserFeign;
import com.changgou.user.pojo.Address;
import com.changgou.util.FastDFSClient;
import com.changgou.util.FastDFSFile;
import com.changgou.util.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/wcenter")
public class CenterController {

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private CollectFeign collectFeign;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private AddressFeign addressFeign;

    //给原手机发消息
    @GetMapping("/sendoldphonesms")
    @ResponseBody
    public Result sendoldphonesms(){
        Result result = userFeign.sendoldphonesms();
        return result;
    }

    //校验原手机验证码
    @GetMapping("/checkoldphone")
    @ResponseBody
    public Result checkoldphone(@RequestParam("smsCode") String smsCode){
        Result result = userFeign.checkoldphone(smsCode);
        return result;
    }

    //给新手机发送验证码
    @GetMapping("/sendnewphone")
    @ResponseBody
    public Result sendphonesms(@RequestParam("mobile") String mobile){
        System.out.println("mobile"+mobile);
        Result result = userFeign.sendphonesms(mobile);
        return result;
    }

    //校验新手机验证码
    @GetMapping("/changephone/{mobile}/{smscode}")
    @ResponseBody
    public Result changephone(@PathVariable("mobile") String mobile,@PathVariable("smscode") String smscode){
        Result result = userFeign.changephone(mobile, smscode);
        return result;
    }

    @PostMapping("/changepassword")
    @ResponseBody
    public Result chagepassword(@RequestBody Map map){
        System.out.println(JSON.toJSONString(map));
        Result result = userFeign.changePassword(map);
        return result;
    }

    @RequestMapping("/tocenter")
    public String toCenter(){
        return "center-index";
    }

    @GetMapping("/list")
    @ResponseBody
    public Result<List<Order>> list(){
        Result<List<Order>> result = orderFeign.list();
        return result;
    }

    @GetMapping("/closeOrder")
    @ResponseBody
    public Result closeOrder(@RequestParam("orderId") String orderId){
        Result result = orderFeign.closeOrder(orderId);
        return result;
    }

    @GetMapping("/sendSeller")
    @ResponseBody
    public Result sendSeller(@RequestParam("orderId") String orderId){
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,"15587871533",orderId);
            return new Result(true, StatusCode.OK,"短信发送成功");
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"短信发送失败");
        }

    }

    @GetMapping("/receipt")
    @ResponseBody
    public Result receipt(@RequestParam("orderId") String orderId){
        Result result = orderFeign.receipt(orderId);
        return result;
    }

    @GetMapping("/findList2Pay")
    @ResponseBody
    public Result<List<Order>> findList2Pay(){
        Result<List<Order>> result = orderFeign.findList2Pay();
        return result;
    }

    @GetMapping("/findList2Send")
    @ResponseBody
    public Result<List<Order>> findList2Send(){
        Result<List<Order>> result = orderFeign.findList2Send();
        return result;
    }

    @GetMapping("/findList2Receive")
    @ResponseBody
    public Result<List<Order>> findList2Receive(){
        Result<List<Order>> result = orderFeign.findList2Receive();
        return result;
    }

    @GetMapping("/findList2Evaluate")
    @ResponseBody
    public Result<List<Order>> findList2Evaluate(){
        Result<List<Order>> result = orderFeign.findList2Evaluation();
        return result;
    }

    @GetMapping("/collectList")
    @ResponseBody
    public Result<List<Sku>> collectList(){
        List<Collect> collectList = collectFeign.list().getData();
        List<Sku> skuList = new ArrayList<>();
        for (Collect collect : collectList) {
            Sku sku = skuFeign.findById(collect.getSkuId()).getData();
            skuList.add(sku);
        }
        return new Result<>(true,StatusCode.OK,"查询成功",skuList);
    }

    /**
     * 评价订单
     */
    @GetMapping("/evaluate/{orderId}")
    @ResponseBody
    public Result evaluation(@PathVariable("orderId")String orderId,@RequestParam("buyerRate") String buyerRate ,@RequestParam("evaluation") String evaluation){
        Result result = orderFeign.evaluation(orderId, buyerRate, evaluation);
        return result;
    }

    //上传图片
    @PostMapping("/upload")
    @ResponseBody
    public Result upload(MultipartFile file){
        try {
            //判断文件是否存在
            if (file==null){
                throw new RuntimeException("文件不存在");
            }
            //获取文件的完整名称
            String originalFilename = file.getOriginalFilename();
            if (StringUtils.isEmpty(originalFilename)){
                throw new RuntimeException("文件不存在");
            }
            //获取文件的扩展名称
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            //获取文件内容
            byte[] content = file.getBytes();
            //创建文件上传的封装实体类
            FastDFSFile fastDFSFile = new FastDFSFile(originalFilename,content,extName);
            //基于工具类进行文件上传
            String[] uploadResult = FastDFSClient.upload(fastDFSFile);

            //封装返回结果
            String url=FastDFSClient.getTrackerUrl()+uploadResult[0]+"/"+uploadResult[1];
            //添加图片路径到用户信息
            userFeign.editHeadPic(url);
            System.out.println(url);
            return new Result(true,StatusCode.OK,"文件上传成功",url);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR,"文件上传失败");
    }

    //编辑用户信息
    @PostMapping("/editUserInfo")
    @ResponseBody
    public Result editUserInfo(@RequestBody Map<String,String> map){
        Result result = userFeign.editUserInfo(map);
        return result;
    }

    //加载地址信息
    @GetMapping("/loadAddressList")
    @ResponseBody
    public Result<List<Address>> loadAddressList(){
        Result<List<Address>> list = addressFeign.list();
        return list;
    }

    //地址信息回显
    @GetMapping("/echo/{id}")
    @ResponseBody
    public Result<Address> echo(@PathVariable("id")Integer id){
        Result<Address> result = addressFeign.findById(id);
        return result;
    }

    //删除地址信息
    @DeleteMapping("/deleteAddress/{id}")
    @ResponseBody
    public Result deleteAddress(@PathVariable("id")Integer id){
        return addressFeign.delete(id);
    }

    //设置默认地址
    @PutMapping("/editDefault/{id}")
    @ResponseBody
    public Result editDefault(@PathVariable("id")Integer id){
        return addressFeign.editDefault(id);
    }

    //编辑地址信息
    @PutMapping("/editAddress/{id}")
    @ResponseBody
    public Result editAddress(@RequestBody Address address,@PathVariable("id")Integer id){
        return addressFeign.update(address,id);
    }

    //编辑地址信息
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody Address address){
        return addressFeign.add(address);
    }


    @RequestMapping("/toPay")
    public String toPay(){
        return "center-order-pay";
    }

    @RequestMapping("/toSend")
    public String toSend(){
        return "center-order-send";
    }

    @RequestMapping("/toReceive")
    public String toReceive(){
        return "center-order-receive";
    }

    @RequestMapping("/toEvaluate")
    public String toEvaluate(){
        return "center-order-evaluate";
    }

    @RequestMapping("/toCollect")
    public String toCollect(){
        return "center-collect";
    }

    @RequestMapping("/toSettingInfo")
    public String toSettingInfo(){
        return "center-setting-info";
    }

    @RequestMapping("/toSettingAddress")
    public String toSettingAddress(){
        return "center-setting-address";
    }

    @RequestMapping("/toSettingSafe")
    public String toSettingSafe(){
        return "center-setting-safe";
    }
}
